package github.kawaiior.yuanchuxiuzhen.core.init.event;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.block.Block;
import net.minecraft.block.IGrowable;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Mod.EventBusSubscriber(modid = Yuanchuxiuzhen.MODID)
public class ModPlayerEvent {

    public static int averageLevel = 0;

    @SubscribeEvent
    public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){
        ManaPower.onPlayerJoinWorld(event.getPlayer().world);
    }

    @SubscribeEvent
    public static void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){
        ManaPower.onPlayerLeave(event.getPlayer().world);
    }

    @SubscribeEvent
    public static void onPlayerHarvestBlock(BlockEvent.BreakEvent event){
        if (event.isCanceled() || event.getWorld().isRemote()){
            return;
        }

        Block block = event.getState().getBlock();

        if (!(block instanceof IGrowable)){
            return;
        }

        if (!MathUtil.checkProbability(0.01F)){
            return;
        }

        World world = (World) event.getWorld();
        Item herbItem = getRandomHerb();
        ItemEntity herbEntity = new ItemEntity(EntityType.ITEM, world);
        herbEntity.setItem(new ItemStack(herbItem));
        BlockPos position = event.getPos();
        herbEntity.setPosition(position.getX(), position.getY() + 0.5D, position.getZ());
        world.addEntity(herbEntity);
    }

    private static Item getRandomHerb(){
        float f = MathUtil.RANDOM.nextFloat();
        int level;
        int index;
        if (f <= 0.01F){
            level = 7;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else if (f <= 0.06F){
            level = 6;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else if (f <= 0.15F){
            level = 5;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else if (f <= 0.20F){
            level = 4;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else if (f <= 0.35F){
            level = 3;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else if (f <= 0.60F){
            level = 2;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        else {
            level = 1;
            index = MathUtil.RANDOM.nextInt(3) + 1;
        }
        return getHerb(level, index);
    }

    private static Item getHerb(int level, int index){
        if (level==1){
            if (index==1){
                return ItemInit.HERB_ITEM_0.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_1.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_2.get();
            }
        }
        else if (level==2){
            if (index==1){
                return ItemInit.HERB_ITEM_3.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_4.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_5.get();
            }
        }
        else if (level==3){
            if (index==1){
                return ItemInit.HERB_ITEM_6.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_7.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_8.get();
            }
        }
        else if (level==4){
            if (index==1){
                return ItemInit.HERB_ITEM_9.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_10.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_11.get();
            }
        }
        else if (level==5){
            if (index==1){
                return ItemInit.HERB_ITEM_12.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_13.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_14.get();
            }
        }
        else if (level==6){
            if (index==1){
                return ItemInit.HERB_ITEM_15.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_16.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_17.get();
            }
        }
        else if (level==7){
            if (index==1){
                return ItemInit.HERB_ITEM_18.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_19.get();
            }
            else if (index==3){
                return ItemInit.HERB_ITEM_20.get();
            }
        }
        else if (level==8){
            if (index==1){
                return ItemInit.HERB_ITEM_21.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_22.get();
            }
        }
        else if (level==9){
            if (index==1){
                return ItemInit.HERB_ITEM_23.get();
            }
            else if (index==2){
                return ItemInit.HERB_ITEM_24.get();
            }
        }
        return Items.AIR;
    }

}
